<?php 
//all the class/function includes are managed in the includer file
include("include/includer.php");
//page specific includes
//RSS Feed Parser
require_once('magpierss72/rss_fetch.inc');

//get all the configuration variables (custom function in newzbook.php include)
getConfigVars();

//if the form was just posted back, let's use the form variables - otherwise the ones above will be used
if(isset($_GET['q'])){
    $q = str_replace(' ','+',$_GET['q']);
}
if(isset($_GET['area'])){
    $area = $_GET['area'];
}
if(isset($_GET['matrix_category'])){
    $matrix_category = $_GET['matrix_category'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<HEAD>
    <?php standardHead("Search"); ?>
</HEAD>

<BODY>
<?php internetSecurity(); ?>
<div style="visibility:hidden;display:none;" id="progress"><div id="progress_bg"></div><a name="loading" /><div id="progress_box"><img id="progress_image" style="padding-left:5px;padding-top:5px;" src="images/ajax-loader.gif" alt="loading...">Your request is being processed...</div></div>
<div class="headercontent">
  <?php headerContent(); ?>
</div>
<div class="maincontent">
<?php
if(!is_dir($config_NZBDropPath)) {
  echo "<div class='warningmessage'>The NZB Drop Path directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
}

if($config_enablenzbget == "enable"){
  if(isset($config_NZBgetAppPath) && isset($config_NZBgetConfigPath)){
    if(!is_dir($config_NZBgetAppPath)) {
        echo "<div class='warningmessage'>The NZBGet Application directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
        }
    if(!is_dir($config_NZBgetConfigPath)){
        echo "<div class='warningmessage'>The NZBGet Configuration directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
        }
  }
}
?>
  <div class="block">
    <h2>Search</h2>
    <a href="rssbrowser.php" style="float:right;font-size:smaller;"><img src="images/feed.png" />RSS Browser</a>
    <form action="nzbsearch.php" method="get">
    <input type="text" name="q" id="q" value="<?php if(isset($_GET['q'])){echo $q;}?>"/>
    <?php if($config_newzenabled == "enable"){ ?>
		<br />
		<input type='hidden' name='newzbin' value='off' />
		<input type='checkbox' name='newzbin' <?php if(!isset($_GET['newzbin']) || $_GET['newzbin'] == 'on'){echo "CHECKED"; } ?> />Newzbin: 
		<select name="area" size="1">
			<option <?php if($area == "-1"){echo "selected ";}?>value="-1">Everything</option>
			<optgroup label="Categories">
			  <option <?php if($area == "c.0"){echo "selected ";}?>value="c.0" label="Unknown">Unknown</option>
			  <option <?php if($area == "c.11"){echo "selected ";}?>value="c.11" label="Anime">Anime</option>
			  <option <?php if($area == "c.1"){echo "selected ";}?>value="c.1" label="Apps">Apps</option>
			  <option <?php if($area == "c.13"){echo "selected ";}?>value="c.13" label="Books">Books</option>
			  <option <?php if($area == "c.2"){echo "selected ";}?>value="c.2" label="Consoles">Consoles</option>
			  <option <?php if($area == "c.15"){echo "selected ";}?>value="c.15" label="Discussions">Discussions</option>
			  <option <?php if($area == "c.10"){echo "selected ";}?>value="c.10" label="Emulation">Emulation</option>
			  <option <?php if($area == "c.4"){echo "selected ";}?>value="c.4" label="Games">Games</option>
			  <option <?php if($area == "c.5"){echo "selected ";}?>value="c.5" label="Misc">Misc</option>
			  <option <?php if($area == "c.6"){echo "selected ";}?>value="c.6" label="Movies">Movies</option>
			  <option <?php if($area == "c.7"){echo "selected ";}?>value="c.7" label="Music">Music</option>
			  <option <?php if($area == "c.12"){echo "selected ";}?>value="c.12" label="PDA">PDA</option>
			  <option <?php if($area == "c.14"){echo "selected ";}?>value="c.14" label="Resources">Resources</option>
			  <option <?php if($area == "c.8"){echo "selected ";}?>value="c.8" label="TV">TV</option>
			</optgroup>
			<?php
			if($config_saved_searches != ""){
				echo "<optgroup label='SavedSearches'>";
				  $savedsearches = explode(",",$config_saved_searches);
				  foreach($savedsearches as $search){
					  //break out the ID and text
					  list($searchID, $searchname) = explode(":",$search);
					  //create the option
					  echo "<option ";
					  //if it's selected, mark it as so
					  if($area == "ss.".$searchID){echo "selected ";}
					  //fill out the details of the option
					  echo "value='ss.$searchID' label='$searchname'>$searchname</option>";
				  }
				echo "</optgroup>";
			}
			?>
		</select>
	<?php } ?>
	<?php if($config_nzbmatrixenabled == "enable"){ ?>
		<br />
		<input type='hidden' name='nzbmatrix' value='off' />
		<input type='checkbox' name='nzbmatrix' <?php if(!isset($_GET['nzbmatrix']) || $_GET['nzbmatrix'] == 'on'){echo "CHECKED"; } ?> />NZBMatrix:
		<?php
		//if I need to redo category support (manually) for nzbmatrix I can find/replace using these expressions:
		//FIND: value\=\"([0-9]+)\"
		//REPLACE: value="\1" <php if($matrix_category == "\1"){echo "SELECTED";}> //don't forget to put the ? marks in
		?>
		<select name="matrix_category">
			<option value="0" <?php if($matrix_category == "0"){echo "SELECTED";}?> >(Everything)</option> 
			<option value="movies-all" style="font-weight: bold;" <?php if($matrix_category == "movies-all"){echo "SELECTED";}?> >Movies: ALL</option> 
			<option value="1" <?php if($matrix_category == "1"){echo "SELECTED";}?> >&nbsp;&nbsp;Movies: DVD</option> 
			<option value="2" <?php if($matrix_category == "2"){echo "SELECTED";}?> >&nbsp;&nbsp;Movies: Divx/Xvid</option> 
			<option value="54" <?php if($matrix_category == "54"){echo "SELECTED";}?> >&nbsp;&nbsp;Movies: BRRip</option> 
			<option value="42" <?php if($matrix_category == "42"){echo "SELECTED";}?> >&nbsp;&nbsp;Movies: HD (x264)</option> 
			<option value="50" <?php if($matrix_category == "50"){echo "SELECTED";}?> >&nbsp;&nbsp;Movies: HD (Image)</option> 
			<option value="48" <?php if($matrix_category == "48"){echo "SELECTED";}?> >&nbsp;&nbsp;Movies: WMV-HD</option> 
			<option value="3" <?php if($matrix_category == "3"){echo "SELECTED";}?> >&nbsp;&nbsp;Movies: SVCD/VCD</option> 
			<option value="4" <?php if($matrix_category == "4"){echo "SELECTED";}?> >&nbsp;&nbsp;Movies: Other</option> 
			<option value="tv-all" style="font-weight: bold;" <?php if($matrix_category == "tv-all"){echo "SELECTED";}?> >TV: ALL</option> 
			<option value="5" <?php if($matrix_category == "5"){echo "SELECTED";}?> >&nbsp;&nbsp;TV: DVD</option> 
			<option value="6" <?php if($matrix_category == "6"){echo "SELECTED";}?> >&nbsp;&nbsp;TV: Divx/Xvid</option> 
			<option value="41" <?php if($matrix_category == "41"){echo "SELECTED";}?> >&nbsp;&nbsp;TV: HD</option> 
			<option value="7" <?php if($matrix_category == "7"){echo "SELECTED";}?> >&nbsp;&nbsp;TV: Sport/Ent</option> 
			<option value="8" <?php if($matrix_category == "8"){echo "SELECTED";}?> >&nbsp;&nbsp;TV: Other</option> 
			<option value="docu-all" style="font-weight: bold;" <?php if($matrix_category == "docu-all"){echo "SELECTED";}?> >Documentaries: ALL</option> 
			<option value="9" <?php if($matrix_category == "9"){echo "SELECTED";}?> >&nbsp;&nbsp;Documentaries: STD</option> 
			<option value="53" <?php if($matrix_category == "53"){echo "SELECTED";}?> >&nbsp;&nbsp;Documentaries: HD</option> 
			<option value="games-all" style="font-weight: bold;" <?php if($matrix_category == "games-all"){echo "SELECTED";}?>>Games: ALL</option> 
			<option value="10" <?php if($matrix_category == "10"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: PC</option> 
			<option value="11" <?php if($matrix_category == "11"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: PS2</option> 
			<option value="43" <?php if($matrix_category == "43"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: PS3</option> 
			<option value="12" <?php if($matrix_category == "12"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: PSP</option> 
			<option value="13" <?php if($matrix_category == "13"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: Xbox</option> 
			<option value="14" <?php if($matrix_category == "14"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: Xbox360</option> 
			<option value="56" <?php if($matrix_category == "56"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: Xbox360 (Other)</option> 
			<option value="15" <?php if($matrix_category == "15"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: PS1</option> 
			<option value="16" <?php if($matrix_category == "16"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: Dreamcast</option> 
			<option value="44" <?php if($matrix_category == "44"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: Wii</option> 
			<option value="51" <?php if($matrix_category == "51"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: Wii VC</option> 
			<option value="45" <?php if($matrix_category == "45"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: DS</option> 
			<option value="46" <?php if($matrix_category == "46"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: GameCube</option> 
			<option value="17" <?php if($matrix_category == "17"){echo "SELECTED";}?> >&nbsp;&nbsp;Games: Other</option> 
			<option value="apps-all" style="font-weight: bold;" <?php if($matrix_category == "apps-all"){echo "SELECTED";}?> >Apps: ALL</option> 
			<option value="18" <?php if($matrix_category == "18"){echo "SELECTED";}?> >&nbsp;&nbsp;Apps: PC</option> 
			<option value="19" <?php if($matrix_category == "19"){echo "SELECTED";}?> >&nbsp;&nbsp;Apps: Mac</option> 
			<option value="51" <?php if($matrix_category == "51"){echo "SELECTED";}?> >&nbsp;&nbsp;Apps: Portable</option> 
			<option value="20" <?php if($matrix_category == "20"){echo "SELECTED";}?> >&nbsp;&nbsp;Apps: Linux</option> 
			<option value="55" <?php if($matrix_category == "55"){echo "SELECTED";}?> >&nbsp;&nbsp;Apps: Phone</option> 
			<option value="21" <?php if($matrix_category == "21"){echo "SELECTED";}?> >&nbsp;&nbsp;Apps: Other</option> 
			<option value="music-all" style="font-weight: bold;" <?php if($matrix_category == "music-all"){echo "SELECTED";}?> >Music: ALL</option> 
			<option value="22" <?php if($matrix_category == "22"){echo "SELECTED";}?> >&nbsp;&nbsp;Music: MP3 Albums</option> 
			<option value="47" <?php if($matrix_category == "47"){echo "SELECTED";}?> >&nbsp;&nbsp;Music: MP3 Singles</option> 
			<option value="23" <?php if($matrix_category == "23"){echo "SELECTED";}?> >&nbsp;&nbsp;Music: Lossless</option> 
			<option value="24" <?php if($matrix_category == "24"){echo "SELECTED";}?> >&nbsp;&nbsp;Music: DVD</option> 
			<option value="25" <?php if($matrix_category == "25"){echo "SELECTED";}?> >&nbsp;&nbsp;Music: Video</option> 
			<option value="27" <?php if($matrix_category == "27"){echo "SELECTED";}?> >&nbsp;&nbsp;Music: Other</option> 
			<option value="28" <?php if($matrix_category == "28"){echo "SELECTED";}?>  style="font-weight: bold;">Anime: ALL</option> 
			<option value="other-all" style="font-weight: bold;" <?php if($matrix_category == "other-all"){echo "SELECTED";}?> >Other: ALL</option> 
			<option value="49" <?php if($matrix_category == "49"){echo "SELECTED";}?> >&nbsp;&nbsp;Other: Audio Books</option> 
			<option value="33" <?php if($matrix_category == "33"){echo "SELECTED";}?> >&nbsp;&nbsp;Other: Emulation</option> 
			<option value="34" <?php if($matrix_category == "34"){echo "SELECTED";}?> >&nbsp;&nbsp;Other: PPC/PDA</option> 
			<option value="26" <?php if($matrix_category == "26"){echo "SELECTED";}?> >&nbsp;&nbsp;Other: Radio</option> 
			<option value="36" <?php if($matrix_category == "36"){echo "SELECTED";}?> >&nbsp;&nbsp;Other: E-Books</option> 
			<option value="37" <?php if($matrix_category == "37"){echo "SELECTED";}?> >&nbsp;&nbsp;Other: Images</option> 
			<option value="38" <?php if($matrix_category == "38"){echo "SELECTED";}?> >&nbsp;&nbsp;Other: Mobile Phone</option> 
			<option value="39" <?php if($matrix_category == "39"){echo "SELECTED";}?> >&nbsp;&nbsp;Other: Extra Pars/Fills</option> 
			<option value="40" <?php if($matrix_category == "40"){echo "SELECTED";}?> >&nbsp;&nbsp;Other: Other</option> 
		</select>
	<?php } ?>
	<br />
    <input type="submit" value="Search" onclick="return loadSubmit()" />
    </form>
  </div>
<?php if($config_newzenabled == "enable" && ($config_newzuname == "" || $config_newzpw == "")){ die("<div class='warningmessage'>Username and/or password are not set. Please set the username/password on the <a href='index.php'>configuration page</a>.</div>"); }
//if(preg_match('/^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$/',$config_newzuname)){die("<div class='warningmessage'>Sorry, an e-mail address cannot currently be used for authentication with the Newzbin search. Please enter your Newzbin username in the configuration.</div>");}?>
<?php
//get the search query variable
if(isset($_GET['q'])){
  //if($q==""){die("<div class='infomessage'>Empty searches are disabled.</div>");}
  echo "<div class='block'>
    <h2>Results</h2>";
      echo "You searched for <font size='+1'><strong>$q</strong></font>.<br /><br />";
	  if(!isset($_GET['newzbin']) && !isset($_GET['nzbmatrix'])){ echo "<div class='warningmessage'>You don't have an indexer enabled. Check your configuration.</div>"; }
	  /* ---- NEWZBIN SEARCH ----- *\
	  \* ------------------------- */
	  if(isset($_GET['newzbin'])){
		 if($_GET['newzbin']=="on"){
		  //post the search to newzbin    
		  $url = 'http://'.$config_newzuname.':'.$config_newzpw.'@www.newzbin.com/search/query/?q='.$q.'&area='.$area.'&fpn=p&searchaction=Go&feed=rss&hauth=1';
		  $rss = fetch_rss( $url );
		  //parse out the fields and create the HTML for the page
		  foreach ($rss->items as $item) {      
			//create the HTML output
			echo "<div class='nzbreport'>";
			//Report Title
			preg_match("/\(Size\: (.+)\)/",$item['description'],$matches);
			echo "<div class='reporttitle'>".$item['title']."<div class='reporttitle_smaller'>(".$matches[1].")</div></div><br />";
			//Report Description - Attributes
			$tmpVar = explode("<li>",$item['description']);
			echo str_replace("</li>","",$tmpVar[2])."<br />";    
				//get the category from the attributes
				preg_match("/Category - (.*)/",$tmpVar[2],$newzbin_category);
				$newzbin_category = $newzbin_category[1];
			//Report Action Links
			echo "<div class='nzbactions'>";
			echo "<a href='".$item['guid']."'>View Report on Newzbin</a> | ";
			//if nzbget is enabled, lets send the user to the nzbget page
			if($config_enablenzbget == "enable"){ 
				echo "<a href='nzbget.php?reportid=".$item['report']['id']."&category=".$newzbin_category."' onclick='return loadSubmit()'>Download Now</a>"; 
			}
			else { 
				echo "<a href='index.php?action=reportactual&reportid=".$item['report']['id']."&category=".$newzbin_category."' onclick='return loadSubmit()'>Download Now</a>"; 
			}
			if(isset($item['report']['nfo_link']) || isset($item['report']['moreinfo'])){
				echo " | ";
			}
			if(isset($item['report']['nfo_link'])){
				echo "(<a href='".$item['report']['nfo_link']."'>NFO</a>) ";
			}
			if(isset($item['report']['moreinfo'])){
				echo "(<a href='".$item['report']['moreinfo']."'>More Info</a>)";        
			}
			echo "</div>";
			//echo "Description: $rss_description <br />";
			echo "</div><br />";
		  }
		 }
		}

	  /* ---- NZBmatrix SEARCH ----- *\
	  \* --------------------------- */
	  if(isset($_GET['nzbmatrix'])){
		 if($_GET['nzbmatrix']=="on"){
		  //post the search to nzbmatrix    
		  $url = 'http://nzbmatrix.com/api-nzb-search.php?search='.$q.'&catid='.$matrix_category.'&num=20&username='.$config_nzbmatrixuname.'&apikey='.$config_nzbmatrixapikey;
		  $matrix_results = file_get_contents( $url );
		
		  //first check for errors
		  if($matrix_results == FALSE){ echo "<div class='warningmessage'>The request was not processed by NZBMatrix. Is the NZBMatrix API server available?</div>"; }
		  if(substr($matrix_results, 0, 6) == 'error:'){
			  echo "<div class='warningmessage'>";
			  switch ($matrix_results) {
				case "error:invalid_login":
					echo "<div class='warningmessage'>NZBMatrix Error: There is a problem with the username you have provided.</div>";
					break;
				case "error:invalid_api":
					echo "<div class='warningmessage'>NZBMatrix Error: There is a problem with the API Key you have provided.</div>";
					break;
				case "error:invalid_nzbid":
					echo "<div class='warningmessage'>NZBMatrix Error: There is a problem with the NZBid supplied.</div>";
					break;
				case "error:vip_only":
					echo "<div class='warningmessage'>NZBMatrix Error: You need to be VIP or higher to access.</div>";
					break;
				case "error:disabled_account":
					echo "<div class='warningmessage'>NZBMatrix Error: User Account Disabled.</div>";
					break;
				case "error:no_nzb_found":
					echo "<div class='warningmessage'>NZBMatrix Error: No NZB found.</div>";
					break;
				case "error:no_scasearch":
					echo "<div class='warningmessage'>NZBMatrix Error: No search query.</div>";
					break;
				case "error:nothing_found":
					echo "<div class='warningmessage'>NZBMatrix Error: No Results Found.</div>";
					break;
				case "error:please_wait_60":
					echo "<div class='warningmessage'>You must wait 60 seconds between NZBMatrix searches. Try again later.</div>";
					break;
				default:
					echo $matrix_results;
					//Should continue below and split out the results
			  }echo "</div>";
		  }
		  else{
			  $matrix_results = explode("|\n",$matrix_results);
			  array_pop($matrix_results);
			  //parse out the fields and create the HTML for the page
			  foreach ($matrix_results as $report) {     
				echo "<div class='nzbreport'>";
				//convert the HTML encoded items back to their respective symbols
				//echo "<pre style='height:150px;overflow-y:scroll;'>$report</pre>";
				$report = html_entity_decode($report);
				//echo "<pre style='height:150px;overflow-y:scroll;'>$report</pre>";
				
				//break out the properties for each report
				$report = explode(";\n",$report);
				array_pop($report);
				foreach($report as $property){
					//echo "<strong>$property</strong><br />";
					//split out each property name from the property value and assign a variable
					$property = explode(":",$property);
					
					//echo "<pre style='height:25px;'>$property[0] is $property[1]</pre>";
					
					$var_name = $property[0];
					$var_value = $property[1];
					
					switch ($var_name) {
						case "NZBID": $matrix_nzbid = $var_value; break;
						case "NZBNAME": $matrix_nzbname = $var_value; break;
						case "LINK": $matrix_link = $var_value; break;
						case "SIZE": $matrix_size = $var_value; break;
						case "INDEX_DATE": $matrix_index_date = $var_value; break;
						case "USENET_DATE": $matrix_usenet_date = $var_value;break;
						case "CATEGORY": $matrix_category = $var_value;break;
						case "GROUP": $matrix_group = $var_value;break;
						case "COMMENTS": $matrix_comments = $var_value;break;
						case "HITS": $matrix_hits = $var_value;break;
						case "NFO": $matrix_nfo = $var_value;break;
						case "REGION": $matrix_region = $var_value;break;
						//case default: //do nothing
					}
				}
				//convert the filesize from bytes to human readable (MB, KB, etc)
				$i=0;
				$iec = array("B", "Kb", "MB", "GB", "TB");
				while (($matrix_size/1024)>1) {
					$matrix_size=$matrix_size/1024;
					$i++;}
				$matrix_size = round($matrix_size,1)." ".$iec[$i];
				
				//convert the REGION from a number to human readable
				switch ($matrix_region){
					case 0:$matrix_region = "Unknown";break;
					case 1:$matrix_region = "PAL";break;
					case 2:$matrix_region = "NTSC";break;
					case 3:$matrix_region = "FREE";break;
				}
		
				//now let's build the HTML for the NZB (report)
				//header
				echo "<div class='reporttitle'>".$matrix_nzbname."<div class='reporttitle_smaller'>(".$matrix_size.")</div></div><br />";
				//descriptive / attributes
				echo "<strong>Category:</strong> ".$matrix_category." <br/>";
				echo "<strong>Group:</strong> $matrix_group <br />";
				if($matrix_region != "Unknown"){ echo "<strong>Region:</strong> $matrix_region <br />"; }
				echo "<strong>ID:</strong> $matrix_nzbid | <strong>Index Date:</strong> $matrix_index_date | <strong>Usenet Date</strong> $matrix_usenet_date | $matrix_comments comments, $matrix_hits hits";
				//actions (links)
				echo "<div class='nzbactions'>";
				echo "<a href='http://$matrix_link'>View Report</a> | ";
				if($config_enablenzbget == "enable"){ 
				echo "<a href='nzbget.php?reportid=".$matrix_nzbid."&indexer=nzbmatrix&reportName=".urlencode($matrix_nzbname)."&category=".$matrix_category."' onclick='return loadSubmit()'>Download Now</a>"; 
				}
				else { 
					echo "<a href='index.php?action=reportactual&reportid=".$matrix_nzbid."&indexer=nzbmatrix&reportName=".urlencode($matrix_nzbname)."&category=".$matrix_category."' onclick='return loadSubmit()'>Download Now</a>"; 
				}
				echo "| (NFO: $matrix_nfo)";
				echo "</div>";
				//end of the report (close the div and add a space)
				echo "</div><br />";
			  }
		  }
		 }
		}
  echo "</div>";
}
?>
</div>
</BODY>